package com.bw.goods.controller;


import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.common.ResponseResult;
import com.bw.common.aliPay.AlipayConfig;
import com.bw.common.idwork.IdWorker;
import com.bw.goods.entity.EGoods;
import com.bw.goods.entity.EOrder;
import com.bw.goods.entity.EOrderReq;
import com.bw.goods.service.IEGoodsService;
import com.bw.goods.service.IEOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xingjinning
 * @since 2020-07-05
 */
@RestController
@RequestMapping("/order")
public class EOrderController {

    @Autowired
    private IEOrderService orderService;
    @Autowired
    private IEGoodsService goodsService;
    @Autowired
    private IdWorker idWorker;
    @PostMapping("save")
    public ResponseResult saveOrder(@RequestBody EOrder order){

        ResponseResult result = ResponseResult.SUCCESS();
        try {
            order.setCode(idWorker.nextId());
            order.setState(0);
            order.setOtime(new Date());
            orderService.saveOrUpdate(order);
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setMessage("保存失败");
            result.setSuccess(false);
        }
        return result;
    }
    @PostMapping("list/{pagenum}/{pagesize}/{uid}")
    public ResponseResult list(@PathVariable("pagenum")int pagenum, @PathVariable("pagesize")int pagesize, @PathVariable("uid")long uid, @RequestBody EOrderReq orderReq){

        ResponseResult result = ResponseResult.SUCCESS();
        PageHelper.startPage(pagenum,pagesize);
        QueryWrapper queryWrapper = new QueryWrapper();
        try {
            queryWrapper.eq("uid",uid);
            if(orderReq.getStarttime()!=null ||orderReq.getEndtime()!=null){
                queryWrapper.between("otime",orderReq.getStarttime(),orderReq.getEndtime());
            }
            List list = orderService.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(list);
            result.setMessage("查询成功");
            result.setResult(pageInfo);
        }catch (Exception e){
            result.setMessage("查询失败");
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("payMoney")
    public ResponseResult list(@RequestBody EOrder order){

        ResponseResult result = ResponseResult.SUCCESS();

        QueryWrapper queryWrapper = new QueryWrapper();
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

            //设置请求参数
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            alipayRequest.setReturnUrl(AlipayConfig.return_url);
            alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

            alipayRequest.setBizContent("{\"out_trade_no\":\""+ order.getCode() +"\","
                    + "\"total_amount\":\""+ order.getOprice() +"\","
                    + "\"subject\":\""+ "支付钱" +"\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

            //请求
            String res = alipayClient.pageExecute(alipayRequest).getBody();
            order.setState(1);
            EGoods eGoods = goodsService.getById(order.getGid());
            eGoods.setGnum(eGoods.getGnum()-order.getNum());
            goodsService.saveOrUpdate(eGoods);
            orderService.saveOrUpdate(order);
            result.setResult(res);
        }catch (Exception e){
            result.setMessage("查询失败");
            result.setSuccess(false);
        }
        return result;
    }
}
